/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.cmict.data.form.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageParam;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.form.api.entity.SysFormEdition;
import com.cmict.data.form.api.entity.dataobject.SysFormDO;
import com.cmict.data.form.api.mapper.SysFormEditionMapper;
import com.cmict.data.form.api.mapper.SysFormMapper;
import com.cmict.data.form.api.service.SysFormEditionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

/**
 * 表单版本类
 * @author qiaoyu
 * @date 2022-04-06
 */
@Service("sysFormEditionService")
@RequiredArgsConstructor
@Slf4j
public class SysFormEditionServiceImpl extends ServiceImpl<SysFormEditionMapper, SysFormEdition> implements SysFormEditionService {

    @Autowired
    SysFormEditionMapper sysFormEditionMapper;
    @Autowired
    SysFormMapper sysFormMapper;

    @Override
    public PageResult<SysFormEdition> getFormEditionPage(PageParam pageParam) {
        return sysFormEditionMapper.selectPage(pageParam,null);
    }

    @Override
    public SysFormEdition getSysFormEdition(Long id) {
        return sysFormEditionMapper.selectById(id);
    }

    @Override
    public Long createSysFormEdition(Long id) {
        SysFormDO sysForm = sysFormMapper.selectById(id);
        SysFormEdition sysFormEdition = new SysFormEdition();
        BeanUtils.copyProperties(sysForm,sysFormEdition);
        sysFormEdition.setFormId(sysForm.getId());
        sysFormEdition.setId(null);
        sysFormEditionMapper.insert(sysFormEdition);
        return sysFormEdition.getId();
    }

    @Override
    public CommonResult returnSysFormEdition(Long id) {
        SysFormEdition sysFormEdition = sysFormEditionMapper.selectById(id);
        if(ObjectUtil.isNotEmpty(sysFormEdition)){
            SysFormDO sysForm = sysFormMapper.selectById(sysFormEdition.getFormId());
            if(ObjectUtil.isNotEmpty(sysForm)){
                BeanUtils.copyProperties(sysFormEdition,sysForm);
                sysForm.setId(sysFormEdition.getFormId());
                sysFormMapper.updateById(sysForm);
                return success("已还原");
            }
        }else{
            return success("历史不存在");
        }
        return null;
    }
}
